package com.ruoyi.exam.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.exam.vo.ExamQuestionAnswerVo;
import com.ruoyi.exam.service.IExamQuestionAnswerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 题目答案Controller
 *
 * @author Tellsea
 * @date 2025-05-30
 */
@Api(tags = "题目答案Controller")
@RestController
@RequestMapping("/exam/questionAnswer")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class ExamQuestionAnswerController extends BaseController {

    private final IExamQuestionAnswerService examQuestionAnswerService;

    @ApiOperation("查询题目答案列表")
    @PreAuthorize("@ss.hasPermi('exam:questionAnswer:list')")
    @GetMapping("/list")
    public TableDataInfo<ExamQuestionAnswerVo> list(ExamQuestionAnswerVo entity) {
        return examQuestionAnswerService.queryList(entity);
    }

    @ApiOperation("查询题目答案所有列表")
    @GetMapping("/listAll")
    public AjaxResult listAll(ExamQuestionAnswerVo entity) {
        return AjaxResult.success("查询成功", examQuestionAnswerService.queryAll(entity));
    }

    @ApiOperation("导出题目答案列表")
    @PreAuthorize("@ss.hasPermi('exam:questionAnswer:export')")
    @Log(title = "题目答案", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ExamQuestionAnswerVo entity) {
        List<ExamQuestionAnswerVo> list = examQuestionAnswerService.queryAll(entity);
        ExcelUtil<ExamQuestionAnswerVo> util = new ExcelUtil<>(ExamQuestionAnswerVo.class);
        util.exportExcel(response, list, "题目答案数据");
    }

    @ApiOperation("获取题目答案详细信息")
    @PreAuthorize("@ss.hasPermi('exam:questionAnswer:query')")
    @GetMapping(value = "/getInfo/{answerId}")
    public AjaxResult getInfo(@PathVariable("answerId") Long answerId) {
        return AjaxResult.success("查询成功", examQuestionAnswerService.queryById(answerId));
    }

    @ApiOperation("新增题目答案")
    @PreAuthorize("@ss.hasPermi('exam:questionAnswer:add')")
    @Log(title = "题目答案", businessType = BusinessType.INSERT)
    @PostMapping("add")
    public AjaxResult add(@RequestBody ExamQuestionAnswerVo entity) {
        return toAjax(examQuestionAnswerService.save(entity));
    }

    @ApiOperation("修改题目答案")
    @PreAuthorize("@ss.hasPermi('exam:questionAnswer:edit')")
    @Log(title = "题目答案", businessType = BusinessType.UPDATE)
    @PostMapping("edit")
    public AjaxResult edit(@RequestBody ExamQuestionAnswerVo entity) {
        return toAjax(examQuestionAnswerService.updateById(entity));
    }

    @ApiOperation("删除题目答案")
    @PreAuthorize("@ss.hasPermi('exam:questionAnswer:remove')")
    @Log(title = "题目答案", businessType = BusinessType.DELETE)
	@GetMapping("/remove/{answerIds}")
    public AjaxResult remove(@PathVariable Long[] answerIds) {
        return toAjax(examQuestionAnswerService.removeByIds(Arrays.asList(answerIds)) ? 1 : 0);
    }
}
